﻿(function ()
{
	nWse.stPageInit.cAddEvtHdlr_DocRdy(function()
	{
		var nMvc = nWse.nMvc;
		var nGui = nWse.nGui;

		var stAppCnsl = nWse.stAppCnsl;

		//-------- GUI框架初始化

		// 装载模型
		var l_RootMdl = nMvc.tMdl.sc_Root = new nMvc.tMdl();
		var l_CtgrsMdl = new nMvc.tMdl();
		fInitMdl();

		// 初始化框架，延迟运行
		nGui.stFrmwk.cInit({
			c_RootPutSrc: document.getElementById("k_RootPutSrc"),
			c_Hbps: [320, 1200],
			c_FadeIn: true
		});

		setTimeout(function ()
		{
			nGui.stFrmwk.cRun();
		}, 300);

		//-------- 模型

		function fInitMdl()
		{
			l_RootMdl.cCrtFld("Ctgrs", l_CtgrsMdl);
			l_RootMdl.cCrtFld("CrntCtgr", "hot");

		//	l_RootMdl.cCrtFld("SortBy", "time-");
		//	l_RootMdl.cAddFldEvtHdlr("SortBy", nMvc.tMdl.tEvtName.i_EgnValUpd, fOnSortByChgd);

			var l_SbjcMdl, i;
			for (i=0; i<g_PageData.SbjcSlugs.length; ++i)
			{
				l_SbjcMdl = new nMvc.tMdlClctn(null, false, {
					c_RmtKeyFldKey: "ID",
					c_Pgnt: false,
					c_Sgmt: true,
					c_SegLen: 10
				});
				l_CtgrsMdl.cCrtFld(g_PageData.SbjcSlugs[i], l_SbjcMdl);
			}

			var l_MdlData;
			l_MdlData = {
				"title": {
					"Wse_Val": "",
					"Wse_Optns": {
						"c_Rqrd": 1
					},
					"Wse_Vldtrs": [
						{
							"c_Tpnm": "tStrLenRge",
							"c_Info": "标题长度 2 - 30 个字符",
							"c_Min": 2,
							"c_Max": 30
						}
					]
				}
				,
				"desc": {
					"Wse_Val": "",
					"Wse_Vldtrs": [
						{
							"c_Tpnm": "tStrLenRge",
							"c_Info": "标题长度 0 - 500 个字符",
							"c_Min": 0,
							"c_Max": 500
						}
					]
				}
				,
				"imgs": ["", "", ""]
			};
			l_Mdl = new nMvc.tMdl(l_MdlData, true);
			l_Mdl.cAllFldsAcpErr(true);
			l_RootMdl.cCrtFld("Qstn", l_Mdl);
		}

		//-------- 命令
		(function ()
		{

			stAppCnsl.cReg("chg_crnt_ctgr", function(a_Help, a_Istr, a_Agms)
			{
				if (a_Help) { return a_Istr; }

				var l_Json = stAppCnsl.cPseAgmToJson(a_Agms, 0);
				l_RootMdl.cUpdFld("CrntCtgr", l_Json.c_NewName);
			});

			stAppCnsl.cReg("upd", function(a_Help, a_Istr, a_Agms)
			{
				if (a_Help) { return a_Istr; }

				var l_CtgrSlug = l_RootMdl.cReadFld("CrntCtgr");
				if (!l_CtgrSlug)
				{ return "缺少当前目录"; }

				var l_Mc = l_CtgrsMdl.cReadFld(l_CtgrSlug);
				if (!l_Mc) { return "目录无效！" }

				if (l_Mc.cIsLock())
				{ return "模型集合被锁定！"; }
				if (l_Mc.cDurAsynPgrs())
				{ return "正在异步处理期间……"; }

				// 卸载
				if (l_Mc.cHasFld("Labs")) { l_Mc.cReadFld("Labs").cUlod(); }
				l_Mc.cUlodData();
				var l_Cmd = stAppCnsl.cBldCmd("0.016^apd", l_CtgrSlug, 0, 20);
				stAppCnsl.cIpt(l_Cmd);
			});

			stAppCnsl.cReg("apd", function(a_Help, a_Istr, a_Agms)
			{
				if (a_Help) { return a_Istr; }

				var l_CtgrSlug = l_RootMdl.cReadFld("CrntCtgr");
				if (!l_CtgrSlug)
				{ return "缺少当前目录"; }

				var l_MdlBgn = a_Agms[0];
				var l_MdlAmt = a_Agms[1];

				var l_Mc = l_CtgrsMdl.cReadFld(l_CtgrSlug);
				if (!l_Mc) { return "目录无效！" }

				if (l_Mc.cIsLock())
				{ return "模型集合被锁定！"; }
				if (l_Mc.cDurAsynPgrs())
				{ return "正在异步处理期间……"; }

				// 先锁定
				l_Mc.cLock();
			//	l_RootMdl.cFldRdol("SortBy", true);

				// 异步发出
				l_Mc.cAsynPgrs(0, { c_Kind: "i_Apd" });

				// Ajax
				var l_QryPrms = {
					"c_Slug": l_CtgrSlug,
					"c_Ofst": l_MdlBgn,
					"c_Cpct": l_MdlAmt,
					"c_AppNeedAthr": true,
					"c_AppNeedMeta": true,
				//	"c_NeedReadCount": true,
				//	"c_SortBy": l_RootMdl.cReadFld("SortBy")
				};
				l_QryPrms = {
					"_c": "question",
					"_a": "of_category",
					"json": JSON.stringify(l_QryPrms)
				};

				var l_Ajax = new nWse.tAjax();
				l_Ajax.cRqst({
					c_Url: g_SiteInfo.c_ApiUrl,
					c_Qry: l_QryPrms,
					c_fOnCplt: function()
					{
						l_Mc.cUlok(); // 解锁模型
					//	l_RootMdl.cFldRdol("SortBy", false);
					},
					c_fOnSucs: function()
					{
						var l_Json = l_Ajax.cGetRspsJson();
						var l_Data = l_Json["data"] || [];
						var l_DataLen = l_Data.length;
					//	console.log(l_Json);

						// 追加，忽略段长
						l_Mc.cApdOneSegData(l_Data, true);
					
						//【警告：最后再触发这个事件！否则可能引发间接递归！】
						l_Mc.cAsynPgrs(+1,
							{
								c_Kind: "i_Apd",
								c_Info: (l_DataLen > 0)
									? ('为您找到 <span style="color: red;">' + l_DataLen + '</span> 条数据')
									: "没有更多数据了"
							});
					},
					c_fOnFail: function(a_Info)
					{
						//【警告：最后再触发这个事件！否则可能引发间接递归！】
						l_Mc.cAsynPgrs(-1,
							{
								c_Kind: "i_Apd",
								c_Info: a_Info
							});
					}
				});
			});

			stAppCnsl.cReg("sbmt_qstn", function(a_Help, a_Istr, a_Agms)
			{
				if (a_Help) { return a_Istr; }

				var l_QstnMdl = l_RootMdl.cReadFld("Qstn");

				if (l_QstnMdl.cIsLock() || l_QstnMdl.cDurAsynPgrs())
				{
					return "模型被锁定或正在异步处理期间！";
				}

				var l_ErrInfo = {};
				var l_MsgCtnt = "";
				if (!l_QstnMdl.cVldt(l_ErrInfo))
				{
					if (l_ErrInfo.c_Flds["title"])
					{
						l_MsgCtnt = "请正确填写标题（2-30字）";
					}
					else
					if (l_ErrInfo.c_Flds["desc"])
					{
						l_MsgCtnt = "请正确填写描述（500字以内）";
					}

					if (l_MsgCtnt)
					{
						nWse.nGui.tMsgBox.scOpen({
							c_IconKind: "i_Fail",
							c_TitText: "表单有误",
							c_CtntHtml: l_MsgCtnt,
							c_Overs: ["i_Cls=知道了"],
							c_Cntdn: 5
						});

					//	console.log(l_ErrInfo);
						return "有错误";
					}
				}

				// 提交
				l_QstnMdl.cLock();
				l_QstnMdl.cAsynPgrs(0,
					{
						c_Kind: "i_Sbmt",
						c_Info: ""
					});

				var l_Body = "json=";
				l_Body += JSON.stringify(l_QstnMdl.cToJson());

				var l_Ajax = new nWse.tAjax();
				l_Ajax.cSetRqstHdr_FormUrlEcd();
				l_Ajax.cRqst({
					c_Mthd: "POST",
					c_Url: nWse.fApdUrlQry(g_SiteInfo.c_ApiUrl, "_c=question&_a=new"),
					c_Body: l_Body,
					c_fOnCplt: function()
					{
						l_QstnMdl.cUlok(); // 解锁模型
					},
					c_fOnSucs: function()
					{
						var l_Json = l_Ajax.cGetRspsJson();
						var l_Data = l_Json["data"];

						//【警告：最后再触发这个事件！否则可能引发间接递归！】
						l_QstnMdl.cAsynPgrs(+1,
							{
								c_Kind: "i_Sbmt",
								c_Info: ""
							});
					
						var l_MsgCtnt =
							'<p class="mi_P1">几秒钟后，您的问题将出现在“' + l_Data["categories"] + '”里！</p>' +
							'<p class="mi_P2"><a href="' + l_Data["url"] + '" target="_blank">立即查看</a></p>';

						nWse.nGui.tMsgBox.scOpen({
							c_IconKind: "i_Sucs",
							c_TitText: "提问成功",
							c_CtntHtml: l_MsgCtnt,
							c_Overs: ["i_Cls=知道了"],
							c_Cntdn: 1000
						});	
					},
					c_fOnFail: function(a_Info)
					{
						var l_Json = l_Ajax.cGetRspsJson();

						//【警告：最后再触发这个事件！否则可能引发间接递归！】
						l_QstnMdl.cAsynPgrs(-1,
							{
								c_Kind: "i_Sbmt",
								c_Info: a_Info
							});

						nWse.nGui.tMsgBox.scOpen({
							c_IconKind: "i_Fail",
							c_TitText: "提问失败",
							c_CtntHtml: l_Json["err_msg"],
							c_Overs: ["i_Cls=知道了"],
							c_Cntdn: 5
						});	
					}
				});
			});

		})();
	});
})();